const http=require('http')
const path = require("path");
const fs= require('fs')
const urlgf=require('url')
const self1=require('../usemodule/self')
// console.log(self1,'>>>自定义模块')//还有内置模块和第三方模块
const serve=http.createServer()
serve.on('request',(req,res)=>{
    const {url,method}=req
    // http://127.0.0.1:8080/about.html
    const str='您的請求方式為'+method+'url為'+url
    let content='<h1>404 Not Found</h1>'
     const fpath=path.join(__dirname,url)
    // console.log(__dirname,url,'>>>>.')
    // content=fpath
    console.log(urlgf.parse(url),'>>>');
    // console.log(new URL(url,'http://127.0.0.1'))
    fs.readFile(fpath,'utf8',function(err,data){
        if(!err){
            content=data
            // 防止中文乱码
           // res.setHeader('Content-Type','text/html;charset=utf-8')
            res.write('设置响应体')
            res.statusCode=200
            res.end(content)
        }else{
            content='<h1>404 Not Found</h1>'
            res.end(content)
        }
    })

})
serve.listen('8088',function(){
    console.log('啓動了,http://127.0.0.1:8088')
})
